package com.insurance.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.insurance.dao.PartnerInfoRoleDao;
import com.insurance.entity.PartnerInfoRoleEntity;
import com.insurance.service.PartnerInfoRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service("partnerInfoRoleService")
public class PartnerInfoRoleServiceImpl extends ServiceImpl<PartnerInfoRoleDao, PartnerInfoRoleEntity> implements PartnerInfoRoleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PartnerInfoRoleEntity> page = this.page(
                new Query<PartnerInfoRoleEntity>().getPage(params),
                new QueryWrapper<PartnerInfoRoleEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public boolean saveAndDelete(String partnerId, List<PartnerInfoRoleEntity> partnerInfoRoleEntityList) throws Exception{
        boolean deleteFlag = deletePartnerInfoRoleByPartnerId(partnerId);
        if(!deleteFlag){
            throw new Exception("保存合作方和角色中间表失败, 删除合作方和角色中间表失败");
        }
        boolean saveFlag = this.savePartnerInfoRoleList(partnerInfoRoleEntityList);
        if(!saveFlag){
            throw new Exception("保存合作方和角色中间表失败");
        }
        return true;
    }

    /**
     * 批量保存合作方与角色关系对象
     *
     * @param partnerInfoRoleEntityList 合作方与角色关系对象集合
     * @return
     */
    @Override
    public boolean savePartnerInfoRoleList(List<PartnerInfoRoleEntity> partnerInfoRoleEntityList){
        if(CollectionUtil.isEmpty(partnerInfoRoleEntityList)){
            return false;
        }
        return this.saveBatch(partnerInfoRoleEntityList);
    }

    @Override
    public boolean deletePartnerInfoRoleByPartnerId(String partnerId){
        if(StringUtils.isBlank(partnerId)){
            return true;
        }
        List<PartnerInfoRoleEntity> partnerInfoRoleList = this.getListByPartnerId(partnerId);
        if(CollectionUtil.isNotEmpty(partnerInfoRoleList)){
            List<String> irIdList = partnerInfoRoleList.stream().map(PartnerInfoRoleEntity::getIrId).collect(Collectors.toList());
            return this.removeByIds(irIdList);
        }
        return true;
    }

    /**
     * 获取查询对象
     *
     * @return
     */
    public QueryWrapper<PartnerInfoRoleEntity> getQueryWrapper(){
        QueryWrapper<PartnerInfoRoleEntity> queryWrapper = new QueryWrapper<PartnerInfoRoleEntity>();
        return queryWrapper;
    }

    /**
     * 通过合作方主键获取关系对象集合
     *
     * @param partnerId 合作方主键
     * @return
     */
    @Override
    public List<PartnerInfoRoleEntity> getListByPartnerId(String partnerId){
        if(StringUtils.isBlank(partnerId)){
            return new ArrayList<PartnerInfoRoleEntity>();
        }
        QueryWrapper<PartnerInfoRoleEntity> queryWrapper = getQueryWrapper();
        queryWrapper.eq("partner_id", partnerId);
        return this.list(queryWrapper);
    }

    /**
     * 通过合作方主键获取角色集合
     *
     * @param partnerId 合作方主键
     * @return
     */
    @Override
    public List<String> getRoleStrListByPartnerId(String partnerId){
        List<PartnerInfoRoleEntity> partnerInfoRoleEntityList = getListByPartnerId(partnerId);
        if(CollectionUtil.isEmpty(partnerInfoRoleEntityList)){
            return new ArrayList<String>();
        }
        List<String> roleList = partnerInfoRoleEntityList.stream().map(PartnerInfoRoleEntity::getRoleId).collect(Collectors.toList());
        return roleList;
    }

}